﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bootstrap5.Abstractions
{
    public interface ICssClassBuilder:IDisposable
    {
        ICssClassBuilder Add(string name, bool condition = true);
        ICssClassBuilder Add(IEnumerable<string> cssClasses, bool condition = true);
    }

    public class CssClassBuilder : ICssClassBuilder
    {
        private readonly ICollection<string> _cssBuilder = new HashSet<string>();

        public ICssClassBuilder Add(string name, bool condition = true)
        {
            if (string.IsNullOrEmpty(name))
            {
                return this;
            }

            if (condition)
            {
                _cssBuilder.Add(name);
            }
            return this;
        }

        public ICssClassBuilder Add(IEnumerable<string> cssClasses, bool condition = true)
        {
            if (cssClasses is null)
            {
                throw new ArgumentNullException(nameof(cssClasses));
            }

            foreach (var item in cssClasses)
            {
                Add(item,condition);
            }

            return this;
        }

        public void Dispose()
        {
            _cssBuilder?.Clear();
        }

        public override string ToString() => string.Join(" ", _cssBuilder);
    }
}
